<?php
/**
* newsModel
*/
class newsModel
{
	public $_table = 'news';
	public function count()
	{
		$sql = 'SELECT count(*) FROM '. $this->_table;
		return DB::findResult($sql, 0, 0);
	}
	public function findAll_orderby_dateline()
	{
		$sql = 'SELECT * FROM ' . $this->_table . ' ORDER BY dateline DESC';
		return DB::findAll($sql);
	}
	public function get_news_list()
	{
		$data = $this->findAll_orderby_dateline();
		foreach ($data as $key => $news) {
			$data[$key]['content'] = mb_substr(strip_tags($data[$key]['content']), 0, 200);
			$data[$key]['dateline'] = date('Y-m-d H:i:s', $data[$key]['dateline']);
		}
		return $data;
	}
	public function getnewsinfo($id)
	{
		if (empty($id)) {
			return array();
		} else {
			$id = intval($_GET['id']);
			$sql = 'SELECT * FROM ' . $this->_table . ' WHERE id=' . $id;
			$data =  DB::findOne($sql);
			$data['dateline'] = date('Y-m-d H:i:s', $data['dateline']);
			return $data;
		}
	}
	public function newssubmit($data)
	{
		extract($data);
		if (empty($title) || empty($content)) {
			return 0;
		}
		$title = daddslashes($title);
		$content = daddslashes($content);
		$author = daddslashes($author);
		$from = daddslashes($from);
		$data = array(
			'title'=>$title,
			'content'=>$content,
			'author'=>$author,
			'from'=>$from,
			'dateline'=>time()
		);
		if ($_POST['id'] != '') {
			DB::update($this->_table, $data, intval($_POST['id']));
			return 2;
		} else {
			DB::insert($this->_table, $data);
			return 1;
		}
	}
	public function delnews($id){
		$sql = 'DELETE FROM ' . $this->_table . ' WHERE id = ' . intval($id);
		return DB::query($sql);
	}
}